<template>
  <div>
    <el-upload
      multiple
      name="myfile"
      ref="receivingFile"
      class="upload-demo"
      :auto-upload="false"
      :data="uplodaData"
      :headers="headers"
      :on-preview="handlePreview"
      :action="uplodaUrl"
      :on-remove="handleRemove"
      :file-list="fileUploads"
      v-if="!isNaN(taskMatterId)"
    >
      <el-button size="small" type="primary" icon="el-icon-upload"
        >上传文件</el-button
      >
    </el-upload>
    <el-upload
      multiple
      name="myfile"
      :action="uplodaUrl"
      :headers="headers"
      :data="uplodaData"
      :on-preview="handlePreview"
      :on-remove="handleRemove"
      :file-list="fileUploads"
      v-else
    >
      <el-button
        size="small"
        type="primary"
        icon="el-icon-upload" 
        >上传文件</el-button
      >
    </el-upload>
  </div>
</template>

<script>
import { delUpload } from "@/api/task/upload";
import { getToken } from "@/utils/auth";
export default {
  name: "newUpload",
  props: { 
    tableType: {
      type: String,
      default: "con",
    },
    uplodaData: {
      type: Object,
      default: {},
    },
    taskMatterId: {},
    firstStepFalg: {
      type: Boolean,
      default: true,
    },
    fileUploads: {
      type: Array,
      default: function () {
        return [];
      },
    },
  },
  watch: {
    taskMatterId: function () {
      this.setTaskMatterId();
    }, 
  },

  created() {
    this.setTaskMatterId();
  },
  data() {
    return {
      headers: { Authorization: "Bearer " + getToken() },
      uplodaUrl: _cfg.serverUrl + "/task/upload/add",
    };
  },
  methods: {
    handleRemove(val) {
      if (typeof val.id != "undefined") {
        delUpload(val.id).then((response) => {});
      }
    },
    //设置任务ID
    setTaskMatterId() {
      console.log(isNaN(this.taskMatterId),this.taskMatterId, "this.taskMatterId");
      if (this.taskMatterId) { 
        this.uplodaData.tableType = this.tableType;
      this.uplodaData.contractid = this.taskMatterId;
      }
    },
    //处理下载
    handlePreview(val) {
      this.downloadFile(val.url);
    },

    uploadFile(id) {
    //   this.uplodaData.processid = id;
      this.uplodaData.tableType = this.tableType;
      this.uplodaData.processid = this.taskMatterId;
      this.$nextTick(() => {
        if (this.$refs.receivingFile) {
          this.$refs.receivingFile.submit();
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
/deep/.upload-demo{
    padding: 0;
}
</style>
